package com.woniuxy.auth.controller;

import com.woniuxy.commons.entity.Staff;
import com.woniuxy.commons.service.StaffService;
import com.woniuxy.commons.utils.JwtUtil;
import com.woniuxy.commons.utils.ResponseResult;
import com.woniuxy.commons.utils.ResultStatus;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/auth")
public class AuthController {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private StaffService staffService;

    //认证方法
    @PostMapping("/login")
    public ResponseResult auth(@RequestBody Staff staff, HttpServletResponse response){
        //1.逻辑：通过账号查找账号信息，然后比对密码   自己做   引入MD5加密工具
        System.out.println("前端传的"+staff);
        String username=staff.getUsername();
        Staff staffByName = staffService.getStaffByName(username);
        if(staffByName==null){
            ResponseResult responseResult = new ResponseResult();
            responseResult.setCode(300);
            responseResult.setMessage("无此用户");
            System.out.println("无此用户");
            return responseResult;
        }else {
            if (!staffByName.getPassword().equals(staff.getPassword())) {
                ResponseResult responseResult = new ResponseResult();
                responseResult.setCode(500);
                responseResult.setStatus(ResultStatus.LOGIN_FAILED);
                responseResult.setMessage("账号密码错误");
                System.out.println("账号密码错误");
                return responseResult;
            } else {

                //假设验证成功
                //1.生成token、refreshToken
                String token = JwtUtil.generateToken(staff.getUsername());
                String refreshToken = UUID.randomUUID().toString();

                //2.将token、refreshToken放到Redis中
                Map<String, Object> map = new HashMap<>();
                map.put("token", token);
                map.put("uname", staff.getUsername());

                //以refreshToken作为key map作为value存放到redis中
                redisTemplate.opsForHash().putAll(refreshToken, map);
                //设置refreshToken过期时间
                redisTemplate.expire(refreshToken, JwtUtil.REFRESH_TOKEN_EXPIRE_TIME, TimeUnit.MILLISECONDS);

                //3.将token、refreshToken放到响应头中返回给浏览器
                response.setHeader("authentication", token);
                response.setHeader("refresh", refreshToken);

                //4.向前端暴露自定义的响应头
                response.setHeader("Access-Control-Expose-Headers", "authentication,refresh");

                //
                ResponseResult responseResult = new ResponseResult();
                responseResult.setCode(200);
                responseResult.setStatus(ResultStatus.LOGIN_SUCCESS);
                responseResult.setMessage("登录成功");
                //
                return responseResult;
            }
        }
    }

////    鉴权方法
//    @GetMapping("/hasperms/{username}/{perms}")
//    public ResponseResult hasPerms(
//            @PathVariable("username") String username, @PathVariable("perms") String perms){
//        //通过账号查询到所有的权限信息    5张表
//        List<String> permsList = staffService.getpermissionByName(username);
//
////        List<String> permsList = Arrays.asList(
////                "user:all",
////                "user:find",
////                "user:update",
////                "user:del"
////        );
//        boolean result = permsList.contains(perms);
//        //
//        ResponseResult responseResult = new ResponseResult();
//
//        if (result){
//            responseResult.setCode(200);
//            responseResult.setStatus(ResultStatus.HAVE_PERMS);
//        }else {
//            responseResult.setCode(500);
//            responseResult.setStatus(ResultStatus.NO_PERMS);
//        }
//        //返回鉴定结果
//        return responseResult;
//    }
}
